//
//  ViewController.m
//  封装异步下载
//
//  Created by OnePiece on 15/9/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ViewController.h"
#import "QFRequestManager.h"
#import "UIImageView+WebCache.h"
#import "Model.h"

@interface ViewController ()

@end

@implementation ViewController

{
    NSMutableArray *_arr;
}

-(instancetype)init{
    if (self = [super init]) {
        _arr = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //建一个labbel（大小满屏）
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    //标记为100
    label.tag = 100;
    
    label.backgroundColor = [UIColor grayColor];
    
    //一视图的大小将一个 UIImageView 视图
    UIImageView * imageV = [[UIImageView alloc]initWithFrame:self.view.frame];
    
    //标记为101
    imageV.tag = 101;
   
    //添加
    [self.view addSubview:label];
    //[self.view addSubview:imageV];
    
    
    
    
    //异步下载 调用第三方的方法Finish:^(NSData *data)回调
    [QFRequestManager requestWithUrl:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=2" IsCache:YES Finish:^(NSData *data) {
        
        //解析
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr1=dic[@"applicationld"];
        
        for (NSDictionary *dict in arr1) {
            
            Model *mo1=[[Model alloc]init];
            
            mo1.iconUrl=dict[@"iconUrl"];
            
            mo1.description=dict[@"description"];
            
            [_arr  addObject:mo1];
            
        }
        
        for (int i=0; i<10; i++) {
            
            Model *mo1=_arr[i];
        
        
        
        //在 label 上添加 数据
       // label.text = dic[@"applications"][0][@"description"];
        
            label.text=mo1.description;
            
            //把 代表记的100放在 label 中
            UILabel * label = (id)[self.view viewWithTag:100];
            
            //不限行（自动换行）
            label.numberOfLines = 0;
            //字体自己适应 label
            label.adjustsFontSizeToFitWidth = YES;
            
            [self.view  addSubview:label];
            
        NSLog(@"you are dog");
        
        }
        /*

         
        //图片视图
        UIImageView * imageV = (id)[self.view viewWithTag:101];
        
        //地址给了 str
        NSString * str = dic[@"applications"][0][@"iconUrl"];
        
        //已 地址 添加图片
       // [imageV sd_setImageWithURL:[NSURL URLWithString:str]];
        
        [ imageV  sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"3.png"]];
        */
    }Failed:^{
        
        NSLog(@"you are dog");
        
    }];
     
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
